import Vue from "vue";
import Router from "vue-router";

Vue.use(Router);

/* Layout */
import Layout from "@/layout";

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: "/",
    redirect: { name: "Home" },
    component: () => import("@/layout/index.vue"),
    children: [
      {
        name: "Home",
        path: "home",
        component: () => import("@/views/home/index.vue"),
        hidden: true,
        meta: {
          title: "首页",
        },
      },
    ],
  },
  {
    path: "/login",
    component: () => import("@/views/login/index"),
    hidden: true,
  },
  {
    path: "/404",
    component: () => import("@/views/404"),
    hidden: true,
  },
];

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  {
    path: "/",
    redirect: { name: "Owner" },
    component: () => import("@/layout/index.vue"),
    children: [
      {
        name: "Owner",
        path: "owner",
        component: () => import("@/views/owner/index.vue"),
        meta: {
          title: "业主管理",
          roleList: ["超级管理员", "业主管理"],
          icon: "el-icon-user",
        },
      },
    ],
  },
  {
    path: "/",
    redirect: { name: "Role" },
    component: () => import("@/layout/index.vue"),
    children: [
      {
        name: "Role",
        path: "role",
        component: () => import("@/views/role/index.vue"),
        meta: {
          title: "权限管理",
          roleList: ["超级管理员", "权限管理"],
          icon: "el-icon-s-check",
        },
      },
    ],
  },
  {
    path: "/",
    redirect: { name: "User" },
    component: () => import("@/layout/index.vue"),
    children: [
      {
        name: "User",
        path: "user",
        component: () => import("@/views/user/index.vue"),
        meta: {
          title: "员工管理",
          roleList: ["超级管理员", "员工管理"],
          icon: "el-icon-user-solid",
        },
      },
    ],
  },
  {
    path: "/",
    redirect: { name: "Task" },
    component: () => import("@/layout/index.vue"),
    children: [
      {
        name: "Task",
        path: "task",
        component: () => import("@/views/task/index.vue"),
        meta: {
          title: "任务管理",
          roleList: ["超级管理员", "任务管理"],
          icon: "el-icon-s-flag",
        },
      },
    ],
  },
  {
    path: "/",
    redirect: { name: "Pay" },
    component: () => import("@/layout/index.vue"),
    children: [
      {
        name: "Pay",
        path: "pay",
        component: () => import("@/views/pay/index.vue"),
        meta: {
          title: "缴费管理",
          roleList: ["超级管理员", "缴费管理"],
          icon: "el-icon-s-order",
        },
      },
    ],
  },
  {
    path: "/",
    redirect: { name: "Patrol" },
    component: () => import("@/layout/index.vue"),
    children: [
      {
        name: "Patrol",
        path: "patrol",
        component: () => import("@/views/patrol/index.vue"),
        meta: {
          title: "巡区管理",
          roleList: ["超级管理员", "巡区管理"],
          icon: "el-icon-position",
        },
      },
    ],
  },
  // ------------------------------------------------------------
  {
    path: "/",
    redirect: { name: "CreateRole" },
    component: () => import("@/layout/index.vue"),
    children: [
      {
        name: "CreateRole",
        path: "create-role",
        component: () => import("@/views/role/create.vue"),
        hidden: true,
        meta: {
          title: "创建权限",
          roleList: ["超级管理员", "权限管理"],
          activeMenu: "/role",
        },
      },
    ],
  },
  {
    path: "/",
    redirect: { name: "CreateUser" },
    component: () => import("@/layout/index.vue"),
    children: [
      {
        name: "CreateUser",
        path: "create-user",
        component: () => import("@/views/user/create.vue"),
        hidden: true,
        meta: {
          title: "创建员工",
          roleList: ["超级管理员", "员工管理"],
          activeMenu: "/user",
        },
      },
    ],
  },
  {
    path: "/",
    redirect: { name: "CreateOwner" },
    component: () => import("@/layout/index.vue"),
    children: [
      {
        name: "CreateOwner",
        path: "create-owner",
        component: () => import("@/views/owner/create.vue"),
        hidden: true,
        meta: {
          title: "创建业主",
          roleList: ["超级管理员", "业主管理"],
          activeMenu: "/owner",
        },
      },
    ],
  },
  {
    path: "/",
    redirect: { name: "CreateTask" },
    component: () => import("@/layout/index.vue"),
    children: [
      {
        name: "CreateTask",
        path: "create-task",
        component: () => import("@/views/task/create.vue"),
        hidden: true,
        meta: {
          title: "创建任务",
          roleList: ["超级管理员", "任务管理"],
          activeMenu: "/task",
        },
      },
    ],
  },
  {
    path: "/",
    redirect: { name: "EditRole" },
    component: () => import("@/layout/index.vue"),
    children: [
      {
        name: "EditRole",
        path: "edit-role",
        component: () => import("@/views/role/edit.vue"),
        props: (route) => {
          return {
            formData: route.query,
          };
        },
        hidden: true,
        meta: {
          title: "编辑权限",
          roleList: ["超级管理员", "权限管理"],
          activeMenu: "/role",
        },
      },
    ],
  },
  {
    path: "/",
    redirect: { name: "EditUser" },
    component: () => import("@/layout/index.vue"),
    children: [
      {
        name: "EditUser",
        path: "edit-user",
        component: () => import("@/views/user/edit.vue"),
        props: (route) => {
          return {
            formData: route.query,
          };
        },
        hidden: true,
        meta: {
          title: "编辑员工",
          roleList: ["超级管理员", "员工管理"],
          activeMenu: "/user",
        },
      },
    ],
  },
  {
    path: "/",
    redirect: { name: "EditOwner" },
    component: () => import("@/layout/index.vue"),
    children: [
      {
        name: "EditOwner",
        path: "edit-owner",
        component: () => import("@/views/owner/edit.vue"),
        props: (route) => {
          return {
            formData: route.query,
          };
        },
        hidden: true,
        meta: {
          title: "编辑业主",
          roleList: ["超级管理员", "业主管理"],
          activeMenu: "/owner",
        },
      },
    ],
  },
  {
    path: "/",
    redirect: { name: "EditTask" },
    component: () => import("@/layout/index.vue"),
    children: [
      {
        name: "EditTask",
        path: "edit-task",
        component: () => import("@/views/task/edit.vue"),
        props: (route) => {
          return {
            formData: route.query,
          };
        },
        hidden: true,
        meta: {
          title: "编辑任务",
          roleList: ["超级管理员", "任务管理"],
          activeMenu: "/task",
        },
      },
    ],
  },
  { path: "*", redirect: "/404", hidden: true },
];

const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes,
  });

const router = createRouter();

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}

export default router;
